<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
	</head>

	<body style="padding: 0px;">
		<div id="toolbar" class="receipts_toolbar_style"></div>
		<form id="ff">
			<table id="table" class="displaytable" cellpadding="6" style="padding: 20px">
				<tr>

					<td width="20%">
						单价</td>
					<td width="10%">
						<input type="text" style="width: 100%;" class="easyui-textbox" name="UNIT_PRICE" id="input_UNIT_PRICE" data-options="disabled:true" />
					</td>
					<td width="20%">
						入库时间</td>
					<td width="10%">
						<input type="text" style="width: 100%;" name="IN_DATE" id="input_IN_DATE" class="easyui-datebox" data-options="disabled:true" />
					</td>
					<td width="20%">
						出库库时间</td>
					<td width="10%">
						<input type="text" style="width: 100%;" name="OUT_DATE" id="input_OUT_DATE" class="easyui-datebox" data-options="required:true" />
					</td>
				</tr>
				<tr>
					<td width="20%">
						<h id="star">* </h>出库人</td>
					<td width="10%">
						<input type="text" style="width: 100%;" name="CHARGER" id="input_CHARGER" class="easyui-combobox" data-options="required:true" />
					</td>
					<td width="20%">
						<h id="star">* </h>数量</td>
					<td width="10%">
						<input type="text" style="width: 100%;" name="AMOUNT" id="input_AMOUNT" class="easyui-numberbox" data-options="required:true" />
					</td>

					<td width="20%">
						<h id="star">* </h>冷冻收费</td>
					<td width="10%">
						<input type="text" style="width: 100%;" name="FROZEN_FEE" id="input_FROZEN_FEE" class="easyui-numberbox feeinput" data-options="disabled:true,precision:2" />
					</td>
					<!--<td width="20%">
						<h id="star">* </h>是否收费</td>
					<td width="10%">
						<input class="easyui-combobox" style="width:100%;" type="text" name="ISCHARGE" id="input_ISCHARGE" />
					</td>-->
				</tr>
				<tr>

					<!--<td width="20%">
						<h id="star">* </h>搬运费</td>
					<td width="10%">
						<input type="text" style="width: 100%;" name="HAND_FEE" id="input_HAND_FEE" class="easyui-numberbox feeinput" data-options="precision:2" />
					</td>-->
					<td width="20%">
						其他收费</td>
					<td width="10%">
						<input type="text" style="width: 100%;" name="OTHRE_FEE" id="input_OTHRE_FEE" class="easyui-numberbox feeinput" data-options="precision:2" />
					</td>
					<td width="20%">
						<h id="star">* </h>应收金额</td>
					<td width="10%">
						<input type="text" style="width: 100%;" name="SUM" id="input_SUM" class="easyui-numberbox" data-options="required:true,disabled:true,precision:2" />
					</td>
					<td width="20%">
						<h id="star">* </h>实收金额</td>
					<td width="10%">
						<input type="text" style="width: 100%;" name="REAL_SUM" id="input_REAL_SUM" class="easyui-numberbox" data-options="required:true,precision:2" />
					</td>
				</tr>
				<tr>

				</tr>
			</table>
			<table>
				<tr style="display: none;">
					<td>
						<h id="star">* </h>库房ID</td>
					<td>
						<input type="text" style="width: 100%;" name="STOREHOUSE_ID" id="input_STOREHOUSE_ID" class="easyui-textbox" data-options="required:true" />
					</td>
					<td>入库单ID</td>
					<td>
						<input type="text" style="width: 100%;" name="ORDER_ID" id="input_ORDER_ID" class="easyui-textbox" data-options="required:true" />
					</td>
					<td>出库人ID</td>
					<td>
						<input type="text" style="width: 100%;" name="CHARGER_ID" id="input_CHARGER_ID" class="easyui-textbox" data-options="required:true" />
					</td>
				</tr>
			</table>
			<div id="dlg" class="easyui-dialog" closed="true" style="width:560px;height:400px;padding:10px">
				<iframe id="ifrmEdit" height="100%" width="100%" frameborder="0" scrolling="no"></iframe>
			</div>
			<div id="new_footer"></div>
		</form>
		<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../../JS/common.js"></script>
		<script type="text/javascript" src="../../../JS/FrozenShareFunction/FrozenShare.js"></script>
		<script type="text/javascript" src="../../../JS/MarketShareFunction/DialogShareFunction.js"></script>
	</body>
	<script type="text/javascript">
		var content;
		var resu = getMultiID();
		var id = resu.id;
		var unit_price = resu.unit_price;
		var instoredate = resu.instoredate;
		var type = resu.type;
		var verifyData;
		var currid;
		var isVerify;
		var AddDialog;
		var receipts_type = RECEIPT_UNVERIFY_SYMBOL + RECEIPT_PRINT_SYMBOL + RECEIPT_EXPORT_SYMBOL;
		receipts_type += RECEIPT_SAVE_SYMBOL + RECEIPT_ADD_SYMBOL + RECEIPT_DELETE_SYMBOL + RECEIPT_CHARGE_SYMBOL;
		content = AddReceiptsToolBar(receipts_type, "");
		$("#toolbar").append(content);
		addDialogFooter($("#new_footer"));

		var curre_date = new Date();
		var current_year = curre_date.getFullYear();
		var current_month = curre_date.getMonth() + 1;
		var current_day = curre_date.getDate();
		$('#input_OUT_DATE').val(current_year + '/' + current_month + '/' + current_day);

		initWriteOffAndOutCombobox();

		createIsChargeCombobox();
		innitBox();
		getModiDateAuth();
		$(document).ready(function() {
			if(id != AddFlag) {
				currid = id;
				var _url = RemoteLinkAddress + '/frozen/outorder/findbyid.do?ID=' + currid;
				getRemoteData(_url);
			} else {
				currid = AddFlag;
			}
		})

		function innitBox() {
			$('#input_UNIT_PRICE').val(unit_price)
			$('#input_IN_DATE').val(instoredate);
			$('#input_AMOUNT').numberbox({
				onChange: function(newValue, oldValue) {
					var amount;
					if((amount = parseFloat(newValue))) {
						var price = parseFloat(unit_price);
						$('#input_FROZEN_FEE').numberbox('setValue', price * amount * getMonthNumber($('#input_IN_DATE').datebox('getValue'), $('#input_OUT_DATE').datebox('getValue')));
						$('#input_SUM').numberbox('setValue', (parseFloat($('#input_FROZEN_FEE').numberbox('getValue')) || 0) + (parseFloat($('#input_OTHRE_FEE').numberbox('getValue')) || 0));
						$('#input_REAL_SUM').numberbox('setValue', (parseFloat($('#input_FROZEN_FEE').numberbox('getValue')) || 0) + (parseFloat($('#input_OTHRE_FEE').numberbox('getValue')) || 0));
						
					}
				}
			})
			$('#input_OTHRE_FEE').numberbox({
				onChange: function(newValue, oldValue) {
					$('#input_SUM').numberbox('setValue', (parseFloat($('#input_FROZEN_FEE').numberbox('getValue')) || 0) + (parseFloat($('#input_OTHRE_FEE').numberbox('getValue')) || 0));
					//$('#input_REAL_SUM').numberbox('setValue', (parseFloat($('#input_FROZEN_FEE').numberbox('getValue')) || 0) + (parseFloat($('#input_OTHRE_FEE').numberbox('getValue')) || 0));
				}
			})
			$('#input_OUT_DATE').datebox({
				onChange: function(newValue, oldValue) {
					var price = parseFloat(unit_price);
					var amount = parseFloat($('#input_AMOUNT').numberbox('getValue'));
					$('#input_FROZEN_FEE').numberbox('setValue', price * amount * getMonthNumber($('#input_IN_DATE').datebox('getValue'), $('#input_OUT_DATE').datebox('getValue')));
				}
			})
		}
		//计算月份差
		function getMonthNumber(date1, date2) {
			var date1 = new Date(date1);
			var date2 = new Date(date2);
			var year1 = date1.getFullYear();
			var year2 = date2.getFullYear();
			var month1 = date1.getMonth() + 1;
			var month2 = date2.getMonth() + 1;
			var len = (year2 - year1) * 12 + (month2 - month1);
			var day = date2.getDate() - date1.getDate();
			if(day >= 0) {
				len += 1;
			}
			return len;
		}

		//新建按钮
		function newdata() {
			$("#verifydisplay").empty(); //去掉审核或者未审核的图片
			isVerify = 0; //将状态改为未审核
			currid = AddFlag;
			resetData();
		}

		//
		function save() {
			var isValid = $("#ff").form('validate');
			if(!isValid) {
				$.messager.alert("警告", "有必填项没有填写");
				return;
			}
			var _url;
			if(currid == AddFlag) {
				_url = RemoteLinkAddress + "/frozen/outorder/save.do"
			} else {
				if(isVerify == 1) {
					$.messager.alert("警告", "表单已审核不能修改!");
					return;
				}
				_url = RemoteLinkAddress + "/frozen/outorder/update.do";
			}
			saveData(_url);
		}

		//删除按钮
		function deletedata() {
			if(isVerify == 1) {
				$.messager.alert("警告", "表单已审核，无法删除");
				return;
			}
			if(currid == AddFlag) {
				$.messager.alert("警告", "该页面为新增页面，无法删除");
				return;
			}
			$.messager.confirm("确认", "是否删除", function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/frozen/outorder/delete.do", {
						"ID": currid,
					}, function(result) {
						if(result.msg == "success") {
							$.messager.alert({
								title: '成功',
								msg: '删除成功!'
							});
							reflashDataGrid();
							var parentLocation;
							parentLocation = searchParent("LeasingOperationDetail.html");
							parentLocation.dialogClose();
						} else {
							$.messager.alert({
								title: 'Error',
								msg: result.msg
							});
						}
					}, 'json');
				}
			});
		}

		//审核按钮
		function verifydata() {
			if(isVerify == 1) {
				$.messager.alert("警告", "表单已审核，无须再审");
				return;
			}
			if(currid == AddFlag) {
				$.messager.alert("警告", "该页面为新增页面，不能执行");
				return;
			}
			$.messager.confirm("确认", "是否审核", function(r) {
				if(r) {
					var _data = reVerifyData(verifyData);
					$.ajax({
						type: "post",
						url: RemoteLinkAddress + "/frozen/outorder/verify.do",
						dataType: 'json',
						data: _data,
						success: function(result) {
							if(result.msg == "success") {
								$.messager.alert({
									title: '成功',
									msg: '审核成功!'
								});
								verifyStatus_Market(1, true);
								isVerify = 1;
								reflashDataGrid();
							} else {
								$.messager.alert({
									title: 'Error',
									msg: result.msg
								});
							}
						}
					});
				}
			});
		}

		//弃审按钮
		function unverifydata() {
			if(isVerify == 0) {
				$.messager.alert("警告", "表单已弃审，无须再审");
				return;
			}
			if(currid == AddFlag) {
				$.messager.alert("警告", "该页面为新增页面，不能执行");
				return;
			}
			$.messager.confirm('Confirm', '是否弃审', function(r) {
				if(r) {
					var _data = reVerifyData(verifyData);
					$.ajax({
						type: "post",
						url: RemoteLinkAddress + "/frozen/outorder/unverify.do",
						dataType: 'json',
						data: _data,
						success: function(result) {

							if(result.msg == "success") {
								$.messager.alert({
									title: '成功',
									msg: '弃审成功!'
								});
								verifyStatus_Market(0, true);
								isVerify = 0;
								reflashDataGrid();
							} else {
								$.messager.alert({
									title: 'Error',
									msg: result.msg
								});
							}
						}
					});
				}
			});
		}

		//打印按钮
		function print() { //打印
			if(currid == AddFlag) {
				$.messager.alert('警示', "当前页面为新增页面，不可打印！");
				return;
			}
			if(currid == AddFlag || isVerify != 1) {
				$.messager.alert('警示', "该页面未审核，不可以打印！");
				return;
			}
			var pLocation = "LeasingOperationPrintOutAndRemoveStorage.html?id=" + currid + "&type=out";
			window.open(pLocation);
		}

		//导出
		function exportExcel() {
			if(currid == AddFlag) {
				$.messager.alert("警告", "该订单为新增订单，无法导出Excel！");
				return;
			}
			$.messager.confirm("提示", "确认导出Excel表格吗？", function(r) {
				if(r) {
					$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
						type: "get",
						data: {
							"ID": currid
						},
						url: RemoteLinkAddress + "/frozen/outorder/excelorder.do",
						dataType: "json", //服务器返回的数据类型
						success: function(result) {
							window.location.href = result.path;
						}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
						error: function(xhr, status, error) {
							$.messager.alert("错误", "请求失败!");
						}
					});
				}
			});
		}

		//收费
		function charge() {
			if(currid == AddFlag) {
				$.messager.alert("警告", "当前是新增页面，不能执行！");
				return;
			}

			var pLocation = "UI/FrozenCave/TenantCharge/TenantChargeAdd.html?type=out&id=" + currid;
			$("#ifrmEdit").attr("src", pLocation);
			constructDialog($("#dlg"), "收费", ChargeDialogWidth, ChargeDialogHeight);
			AddDialog.dialog('open');
		}

		function getRemoteData(_url) {
			$.ajax({
				type: "get",
				url: _url,
				dataType: "json",
				success: function(result) {
					if(result != null) {
						verifyData = result;
						isVerify = result.ISVERIFY;
						currid = result.ID;
						getDialogData(result);
						verifyStatus_Market(isVerify, true);
					}
				}
			});
		}

		//刷新datagrid
		function reflashDataGrid() {
			var _url = RemoteLinkAddress + "/frozen/inorder/findbyid.do?ID=" + 　$("#input_ORDER_ID").val();
			var parentLocation;
			parentLocation = searchParent("LeasingOperationDetail.html");
			parentLocation.getRemoteData(_url);
		}

		function reVerifyData(_data) {
			var data = {};
			data.AMOUNT = _data.AMOUNT;
			data.ID = _data.ID;
			data.ORDER_ID = _data.ORDER_ID;
			data.TYPE = _data.TYPE;
			return data;
		}

		//判断数据的完整性
		function isTrueData() {
			var tip = true;
			$("#table input[name]").each(function(index) {
				if($(this).val().trim().length == 0) {
					tip = false;
					return;
				}
			});
			return tip;
		}

		//提交数据(提交至当前入库单)
		function saveData(_url) {
			lockButton("lock");
			var _data = submitDialogData();
			_data.UNIT_PRICE = _data.FROZEN_UNIT;
			if(currid != AddFlag) {
				_data.ID = currid;
			}
			_data.TYPE = "0";
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: _data,
				type: "post",
				success: function(result) {
					lockButton("unlock");
					if(result.msg == "zero") {
						$.messager.alert({
							title: '失败',
							msg: '库存数不足!'
						});
					} else {
						if(result.msg.search("success") > -1) {
							$.messager.alert("消息", "保存成功");
							reflashDataGrid();
							var parentLocation;
							parentLocation = searchParent("LeasingOperationDetail.html");
							parentLocation.dialogClose();
						} else {
							$.messager.alert("提示", "保存失败");
						}
					}
				},
				error: function(result) {
					$.messager.alert("错误", "保存失败");
				}
			});
		}

		function dialogClose() {
			AddDialog.dialog('close');
		}
	</script>

</html>